// 吉用贷-尽职调查表
import ajax from '../../api/ajax'
import { rsadecryptData, attestationCode } from '../../common/js/rsa'
import { SUCCESS_STATUS } from '../../common/js/constant'
import { aesDecrypt } from '../../common/js/crypto'
import { msg } from '../../common/js/util'
export default{
    namespaced: true,
    state: () => ({
      dueDiligenceSurveyFormFlag:0,
      dueDiligenceSurveyForm: {}, // 尽职调查表数据提交总和
      dueDiligenceSurveyFormOne:{}, // 尽职调查表1
      dueDiligenceSurveyFormTwo:{}, // 尽职调查表2
      dueDiligenceSurveyFormThree:{}, // 尽职调查表3
      generateId:'',  // 生成合同模版id
      contractType:'', // 合同类型
      contractNo:'', // 合同id
      contractFields:{}, // 尽职调查初始化数据
      // 尽职调查下拉框数组
      dueDiligenceSurveySignlePickerList: []
    }),
      mutations: {
        JYD_DUE_DILIGENCE_SURVEY_FORM_Flag(state,data) {
          state.dueDiligenceSurveyFormFlag = data
        },
        JYD_DUE_DILIGENCE_SURVEY_FORM(state,data) {
          state.dueDiligenceSurveyForm = data
        },
        JYD_DUE_DILIGENCE_SURVEY_FORM_ONE(state,data) {
            state.dueDiligenceSurveyFormOne = data
        },
        JYD_DUE_DILIGENCE_SURVEY_FORM_TWO(state,data) {
            state.dueDiligenceSurveyFormTwo = data
        },
        JYD_DUE_DILIGENCE_SURVEY_FORM_THREE(state,data) {
            state.dueDiligenceSurveyFormThree = data
        },
        JYD_DUE_DILIGENCE_SURVEY_SIGNLE_PICKER_LIST(state,data) {
          state.dueDiligenceSurveySignlePickerList = data
        },
        JYD_DUE_DILIGENCE_SURVEY_Generate_ID(state,data) {
          state.generateId = data
        },
        JYD_DUE_DILIGENCE_SURVEY_CONTRACT_TYPE(state,data) {
          state.contractType = data
        },
        JYD_DUE_DILIGENCE_SURVEY_CONTRACT_NO(state,data) {
          state.contractNo = data
        },
        JYD_DUE_DILIGENCE_SURVEY_Contract_Fields(state,data) {
          state.contractFields = data
        }

      },
      getters: {
        // 有效证件名称
        validIDNameItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'vali_id_name')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 征信情况评价
        creditEvaluatItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'credit_evaluat')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 个人行为评价
        personBehaviorItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'person_behavior_evaluate')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 还款能力评价
        repaymentAbilityItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'repayment_ability_evaluate')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 贷款用途
        loanPurposeMethodItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'loan_purpose')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 还款来源
        repaymentSourceItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'repayment_source')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 担保方式
        getGuaranteeMethodItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'guarantee_method')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 楼户比
        buildHouseRatioItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'build_hose_ratio')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 使用情况
        usageStatusItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'usage_status')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
        // 不利因素
        adverseFactorsItem (state) {
          let obj = {};
          obj = state.dueDiligenceSurveySignlePickerList.filter(todo =>todo.dictNo == 'explantion_adverse_factors')
          let pickerArr = obj[0].childList;
          let pickerItems = []
            for (let i = 0; i < pickerArr.length; i++) {
                const element = pickerArr[i];
                pickerItems.push({
                    label: element.dictName,
                    value: element.dictNo
                });
            }
          return [pickerItems]
            
        },
      },
      actions: {
        updateSignlePickerList ({ state, commit, rootState }) {
           // 获取字典项
           ajax
           .getDictList({ 
            dictNos: 
            [
               "vali_id_name", 
               "credit_evaluat", 
               "person_behavior_evaluate",
               "repayment_ability_evaluate",
               "repayment_source",
               "loan_purpose",
               "guarantee_method",
               "build_hose_ratio",
               "usage_status",
               "explantion_adverse_factors",
            ] })
           .then((res) => {
           const { responseHead: { responseCode, responseMsg, random = "", sign = "" }, responseBody = {}, } = res;
           if (responseCode === SUCCESS_STATUS) {
               let bsign = `responseCode=${responseCode}responseMsg=${responseMsg}${responseBody}`;
               let data = aesDecrypt(responseBody, rsadecryptData(random))
                   ? JSON.parse(aesDecrypt(responseBody, rsadecryptData(random)))
                   : {};
               console.log("获取下拉框字段数组:", data);
               if (attestationCode(bsign, sign)) {
                commit('JYD_DUE_DILIGENCE_SURVEY_SIGNLE_PICKER_LIST',data)
               }
           }
           else {
               msg(responseCode + ":" + responseMsg);
           }
       }).catch(() => { });
          
        }
      }
  }